﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.SellOrder;

namespace UI.BuyManagement
{
    public partial class FrmAccept : UI.ParentForm.BaseMain
    {
        FrmAcceptMedit edit = null;
        ToolStripButton btnAccept = null;
        ToolStripButton btnPrint = null;
        private ToolStripButton _tspAcceptComplate;

        public FrmAccept(string modulecode, string action)
            : base(modulecode, action)
        {
            InitializeComponent();
        }

        protected override void AddToolButton()
        {
            //收货单录入的功能按钮
            btnAccept = this.CreateToolBtn("W", "收货单录入", new EventHandler(delegate
            {
                DataGridViewRow row = this.GetSelectedRow();
                if (null == row) return;

                OpenEdit("W", "收货单录入", Components.Validation.ToString(row.Cells["listid"].Value));
            }));

            this.tspTop.Items.Add(btnAccept);

            //收货单打印功能按钮
            //btnPrint = this.CreateToolBtn("P", "打印收货单", new EventHandler(delegate
            // {
            //     DataGridViewRow row = this.GetSelectedRow();
            //     if (null == row) return;

            //     OpenEdit("P", "打印收货单", Components.Validation.ToString(row.Cells["listid"].Value));
            // }));

            _tspAcceptComplate = base.CreateToolBtn("C", "收货完成", tspAcceptComplate_Click);
            this.tspTop.Items.Add(_tspAcceptComplate);
        }

        private void tspAcceptComplate_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = this.GetSelectedRow();
            if (null == row) return;

            OpenEdit("C", "收货完成", Components.Validation.ToString(row.Cells["listid"].Value));
        }

        protected override void OnViewClick(ToolStripButton btn, DataGridViewRow row)
        {
            OpenEdit("V", btn.Text, Components.Validation.ToString(row.Cells["listid"].Value));
        }

        private void OpenEdit(string action, string btnText, string code)
        {
            edit = new FrmAcceptMedit(this.ModuleCode, action, GetTitleStyle(this.Text, btnText), code);
            edit.ShowDialog();
            if (edit.IsReload) base.BindGrid();
        }

        protected override void UpdatePager(UI.Common.Pager pager)
        {
            pager.FilterAppName = "a.usercode";
            pager.StrWhere = " a.ordstatus <> '9' ";
        }

        protected override void AfterRowChanged(int rowIndex)
        {
            if (rowIndex == -1 || null == btnAccept || false == btnAccept.Visible ) return;

            string ordstatus = Components.Validation.ToString(this.dgvMain["ordstatus", rowIndex].Value);

            btnAccept.Enabled = false;

            if ("10" == ordstatus) //已经发货,可以打印
            {
                _tspAcceptComplate.Enabled = false;
            }
            else 
            {
                btnAccept.Enabled = true;
                _tspAcceptComplate.Enabled = true;
            }
        }
    }
}
